home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Snippets / TaskManager 2.2.1P / TestPTasks.p < prev    next >
Encoding:
Text File  |  1992-11-24  |  5.5 KB  |  209 lines  |  [TEXT/PJMM]

  1. program TestPTasks;
  2.  
  3. {        Task Manager -- Background processing support}
  4. {        version 2.2}
  5.  
  6. {    This software source package is Copyright © 1990-91 by Michael Hecht. All Rights}
  7. {    Reserved. It may be freely distributed in source or object code format; however,}
  8. {    the source code may not be sold for profit or charged for in any way. The source}
  9. {    code must be distributed as a package including all H files, sample code and}
  10. {    projects, and documentation.}
  11.  
  12. {    I welcome any comments or suggestions that will help me improve or extend the}
  13. {    functionality of the Task Manager. You can reach me at:}
  14.  
  15. {        Internet:        Michael_Hecht@mac.sas.com}
  16. {        AppleLink:        SAS.HECHT}
  17.  
  18. {    Happy Tasking!}
  19.  
  20. {    --Michael Hecht}
  21.  
  22. {    Pascal Conversion by Peter N Lewis <peter@ncrpda.curtin.edu.au>, Aug 1992}
  23.  
  24.     uses
  25.         Tasks;
  26.  
  27.     type
  28.         SlaveRecord = record
  29.                 portRect: Rect;
  30.                 title, msg1, msg2: str31;
  31.                 slaveWindow: WindowPtr;
  32.             end;
  33.         SlavePtr = ^SlaveRecord;
  34.         SlaveHandle = ^SlavePtr;
  35.  
  36.     procedure DrawRandomMsg (wp: WindowPtr; msg1, msg2: str31);
  37.         var
  38.             r: Rect;
  39.             scrollRgn: RgnHandle;
  40.     begin
  41.         SetPort(wp);
  42.  
  43.     { Scroll the screen }
  44.         scrollRgn := NewRgn;
  45.         r := wp^.portRect;
  46.         ScrollRect(r, 0, -16, scrollRgn);
  47.         FillRgn(scrollRgn, white);
  48.         DisposeRgn(scrollRgn);
  49.  
  50.     { Pick one of two messages at random and draw it }
  51.         MoveTo(5, 128);
  52.         if odd(random) then
  53.             DrawString(msg1)
  54.         else
  55.             DrawString(msg2);
  56.     end;
  57.  
  58.     procedure SlaveProc (theSlaveHandle: SlaveHandle);
  59.         var
  60.             theSlave: SlaveRecord;
  61.             slaveWindow: WindowPtr;
  62.             oe: OSErr;
  63.     begin
  64.  
  65.     { Our refCon is really a SlaveHandle; get the record it points to {}
  66.         theSlave := theSlaveHandle^^;
  67.  
  68.     { Create a window for our slave }
  69.         slaveWindow := NewWindow(nil, theSlave.portRect, theSlave.title, TRUE, noGrowDocProc, FrontWindow, TRUE, 0);
  70.  
  71.     { Store the window ptr in our SlaveHandle so the TaskTerm procedure can find it }
  72.         theSlaveHandle^^.slaveWindow := slaveWindow;
  73.  
  74.     { Draw messages forever (the main task will stop us when it's time) }
  75.         while true do begin
  76.         { Let other tasks run }
  77.             oe := TaskYield;
  78.  
  79.         { Draw one of our messages }
  80.             DrawRandomMsg(slaveWindow, theSlave.msg1, theSlave.msg2);
  81.         end;
  82.     end;
  83.  
  84.     procedure SlaveTermProc (theSlaveHandle: SlaveHandle);
  85.     begin
  86.     { Time to close our window (if it was ever created) }
  87.         if theSlaveHandle^^.slaveWindow <> nil then
  88.             CloseWindow(theSlaveHandle^^.slaveWindow);
  89.  
  90.     { SlaveHandle no longer needed }
  91.         DisposHandle(handle(theSlaveHandle));
  92.     end;
  93.  
  94.     function NewSlave (bounds: Rect; title, msg1, msg2: str31): OSErr;
  95.         var
  96.             err: OSErr;
  97.             theSlave: SlaveRecord;
  98.             theSlaveHandle: SlaveHandle;
  99.             trn: integer;
  100.  
  101.     begin
  102.     { Create a new slave; first, initialize a SlaveRecord }
  103.         theSlave.portRect := bounds;
  104.         theSlave.title := title;
  105.         theSlave.msg1 := msg1;
  106.         theSlave.msg2 := msg2;
  107.  
  108.     { Be sure to set this to nil, in case the term proc gets called too early! }
  109.         theSlave.slaveWindow := nil;
  110.  
  111. {     *    Next; convert it to a SlaveHandle, so it can sit in the heap until the}
  112. {     *    slave can retrieve it.}
  113.  
  114.         err := PtrToHand(@theSlave, handle(theSlaveHandle), sizeof(SlaveRecord));
  115.         if err = noErr then begin
  116.         { Create the slave task, using the SlaveHandle as its taskRefCon }
  117.             err := NewTask(@SlaveProc, @SlaveTermProc, longInt(theSlaveHandle), trn);
  118.             if err <> noErr then begin
  119.                 DisposHandle(handle(theSlaveHandle));
  120.             end;
  121.         end;
  122.         NewSlave := err;
  123.     end;
  124.  
  125.     var
  126.         err: OSErr;
  127.         masterWindow, theWindow: WindowPtr;
  128.         r: Rect;
  129.         timeToQuit: Boolean;
  130.         theEvent: EventRecord;
  131.  
  132. begin
  133.     { Initialize the ToolBox }
  134.  
  135.     { Turn on tasking }
  136.     err := InitTasking;
  137.     if err = noErr then begin
  138. { Make slaves }
  139.         SetRect(r, 261, 45, 507, 176);
  140.         err := NewSlave(r, 'Task 1', 'See my Task 1 message?', 'Of course you do—I’m Task 1!');
  141.  
  142.         SetRect(r, 5, 206, 251, 337);
  143.         if err = noErr then
  144.             err := NewSlave(r, 'Task 2', 'THIS is Task 2?', 'Y E S, it is!');
  145.  
  146.         SetRect(r, 261, 206, 507, 337);
  147.         if err = noErr then
  148.             err := NewSlave(r, 'Task 3', 'The great and mighty Task 3.', 'See me??? I’m Task 3!');
  149.  
  150.         if err = noErr then begin
  151.     { Make master window }
  152.             SetRect(r, 5, 45, 251, 176);
  153.             masterWindow := NewWindow(nil, r, 'Master Task', TRUE, noGrowDocProc, POINTER(-1), TRUE, 0);
  154.  
  155.     { A very simple event loop }
  156.             timeToQuit := FALSE;
  157.             while not timeToQuit do begin
  158.                 if WaitNextEvent(everyEvent, theEvent, 0, nil) then begin
  159.  
  160.                     case theEvent.what of
  161.  
  162.                         mouseDown:  begin
  163.                             case FindWindow(theEvent.where, theWindow) of
  164.                                 inGoAway:  begin
  165.                     { Track it }
  166.                                     if TrackGoAway(theWindow, theEvent.where) then begin
  167.                     { It's time to quit if they close the master window }
  168.                                         timeToQuit := masterWindow = theWindow;
  169.                                     end;
  170.                                 end;
  171.                                 inContent, inDrag:  begin
  172.                     { Bring it to the front if need be }
  173.                                     if theWindow <> FrontWindow then
  174.                                         SelectWindow(theWindow);
  175.                                 end;
  176.                                 otherwise
  177.                                     ;
  178.                             end;
  179.                         end;
  180.                         updateEvt:  begin
  181.                 { Clear out any update event }
  182.                             theWindow := WindowPtr(theEvent.message);
  183.                             BeginUpdate(theWindow);
  184.                             EndUpdate(theWindow);
  185.                         end;
  186.                         otherwise
  187.                             ;
  188.                     end;
  189.                 end;
  190.  
  191.         { Allow tasks to run for five ticks }
  192.                 err := RunTasks(5);
  193.  
  194.         { Let the master task do something useful }
  195.                 DrawRandomMsg(masterWindow, 'Obey me—I am the Master!', 'Close me to quit.');
  196.  
  197.     { Continue until it's time to quit or there are no more tasks }
  198.             end;
  199.  
  200.     { Close the master window }
  201.             CloseWindow(masterWindow);
  202.  
  203. {     *    Don't forget to do this! It will dispose of all tasks,}
  204. {     *    thereby calling each one's TaskTerm procedure.}
  205.  
  206.         end;
  207.         err := TermTasking;
  208.     end;
  209. end.